package cn.abcsys.cloud.devops.runtime.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.common.Constant;
import cn.abcsys.cloud.devops.runtime.common.Status;
import cn.abcsys.cloud.devops.runtime.dao.ClusterMapper;
import cn.abcsys.cloud.devops.runtime.dao.EnvMapper;
import cn.abcsys.cloud.devops.runtime.dao.RegistryMapper;
import cn.abcsys.cloud.devops.runtime.entity.Cluster;
import cn.abcsys.cloud.devops.runtime.entity.Env;
import cn.abcsys.cloud.devops.runtime.entity.Registry;
import cn.abcsys.cloud.devops.runtime.model.EnvModel;
import cn.abcsys.cloud.devops.runtime.service.EnvService;
/**
 * 环境管理业务处理
 * @author mayunhao
 *
 */
@Component
public class EnvServiceImpl implements EnvService {
	private static final Logger LOGGER = Logger.getLogger(EnvServiceImpl.class);
	@Autowired
	private EnvMapper envMapper;
	@Autowired
	private ClusterMapper clusterMapper;
	@Autowired
	private RegistryMapper registryMapper;
	@Override
	public Result createEnv(Env env) {
		Result result = null;
		try {
			int issuccess = envMapper.insertEnv(env);
			if(issuccess!=1){
				result = new Result(false, "创建环境失败,原因未知");
			}else{
				result = new Result(true, env.getEnvId(), "创建环境成功");
			}
		} catch (SQLException e) {
			LOGGER.error("创建环境失败:"+e.getMessage());
			result = new Result(false, "创建环境失败："+e.getMessage());
		}
		LOGGER.info("创建成功");
		return result;
	}

	@Override
	public Result updateEnv(Env env) {
		Result result = new Result(true, "更新环境成功");
		try {
			int issuccess = envMapper.updateEnv(env);
			if(issuccess!=1){
				result = new Result(false, "更新环境失败,原因未知");
			}
		} catch (SQLException e) {
			LOGGER.error("更新环境失败:"+e.getMessage());
			result = new Result(false, "更新环境失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result deleteEnv(String envId) {
		Result result = new Result(true, "移除环境成功");
		try {
			String[] envIds = envId.split(Constant.separator);
			for(String id:envIds){
				int issuccess = envMapper.deleteEnv(Integer.valueOf(id));
				if(issuccess!=1){
					LOGGER.error("移除["+id+"]环境失败");
				}
			}
		} catch (SQLException e) {
			LOGGER.error("移除环境失败:"+e.getMessage());
			result = new Result(false, "移除环境失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result getEnvDetail(Integer envId) {
		Result result = null;
		try {
			Env env = envMapper.getEnvDetail(envId);
			if(null!=env){
				result = new Result(true,JSONObject.toJSON(env),"success");
			}else{
				result = new Result(false,"未查到相关结果");
			}
		} catch (SQLException e) {
			LOGGER.error("查询环境详情失败:"+e.getMessage());
			result = new Result(false, "查询失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public GridBean getEnvList(EnvModel envModel) {
		int page = envModel.getPage();
		int rows = envModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<Env> pageObject = new Page<Env>();
		try {
			Map<String,Object> map = new HashMap<>();
			map.put("envName", envModel.getEnvName());
			map.put("clusterStatus", Status.COMMON.NORMAL.ordinal());
			
			PageHelper.startPage(page, rows);
			List<Env> envList = envMapper.selectEnvList(map);
			for(Env cluster: envList){
				pageObject.add(cluster);
			}
			totalpage = ((Page<?>) envList).getPages();
			totalNum = ((Page<?>) envList).getTotal();
		} catch (SQLException e) {
			LOGGER.error("批量查询环境失败:"+e.getMessage());
		}
		GridBean gridBean = new GridBean(page, totalpage, totalNum.intValue(), pageObject);
		return gridBean;
	}

	@Override
	public Result envGlobalInfo(Integer envId) {
		try {
			JSONObject jsonObject = new JSONObject();
			Env env = envMapper.getEnvDetail(envId);
			if(null==env){
				LOGGER.error("环境全局信息查询失败");
				return new Result(false, "环境全局信息查询失败");
			}
			jsonObject.put("global_env", env);
			List<Cluster> clusters = clusterMapper.selectClusterListByEnvId(envId);
			if(null==clusters||clusters.isEmpty()){
				LOGGER.error("环境全局信息查询失败");
				return new Result(false, "环境全局信息查询失败");
			}
			Cluster cluster = clusters.get(0);
			if(null==cluster){
				LOGGER.error("环境全局信息查询失败");
				return new Result(false, "环境全局信息查询失败");
			}
			jsonObject.put("global_cluster", cluster);
			Registry registry =  registryMapper.getRegistryDetail(cluster.getClusterRegistryId());
			if(null==registry){
				LOGGER.error("环境全局信息查询失败");
				return new Result(false, "环境全局信息查询失败");
			}
			jsonObject.put("global_registry", registry);
			return new Result(true, jsonObject, "success");
		} catch (SQLException e) {
			LOGGER.error("环境全局信息查询失败");
			return new Result(false, "环境全局信息查询失败");
		}
	}
}
